<template>
	<view>
		<view class="content" v-if="addressList.length >= 0">
			<view class="list">
				<view class="row" v-for="(row,index) in addressList" :key="index" @tap="select(row)">
					<view class="left">
						<view class="head">
							{{row.head}}
						</view>
					</view>
					<view class="center">
						<view class="name-tel">
							<view class="name">{{row.consignee}}</view>
							<view class="tel">{{row.mobile}}</view>
							<view class="default" v-if="row.isnoDefault === '1'">
								默认
							</view>
						</view>
						<view class="address">
							{{row.allAddress}}
						</view>
					</view>
					<view class="right">
						<view class="icon" @tap.stop="edit(row)">&#xe61d;</view>
					</view>
				</view>
			</view>
		</view>
		<view class="no_address" v-if="addressList.length === 0">
			<empty src="empty2" title="收货地址为空" text="快来添加您的收货地址吧" />
		</view>
		<view class="add">
			<view class="btn" @tap="add">
				<view class="icon">&#xe656;</view>新增地址
			</view>
		</view>
	</view>
</template>
<script>
	import {  
	    mapState, mapActions
	} from 'vuex';
	import addressApis from'@/common/services/address.js'
	import empty from '@/components/empty.vue'
	export default {
		data() {
			return {
				isSelect:false,
				addressList: [],
			};
		},
		computed: {
		    ...mapState(['selectAddress'])
		},
		components:{
			empty	
		},
		onLoad(e) {
			if(e.type=='select'){
				this.isSelect = true;
			}
		},
		onShow(){
			addressApis.getAllAddress().then(res => {
				this.addressList = res.records;
				this.setAddressList({ address: res.records})
			})	
		},
		methods:{
			edit(row){
				uni.navigateTo({
					url: `edit/edit?type=edit&id=${row.id}`
				})
			},
			add(){
				uni.navigateTo({
					url:"edit/edit?type=add"
				})
			},
			select(row){
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if(!this.isSelect){
					return ;
				}
				uni.setStorage({
					key:'selectAddress',
					data:row,
					success() {
						uni.navigateBack();
					}
				})
			},
			...mapActions({
				setAddressList: 'SET_ADDRESS_LIST',
			})
		}
	}
</script>

<style lang="scss">

	view{
		display: flex;
	}
	@font-face {
		font-family: 'iconfont';
		src: url('../../css/font/iconfont.eot');
		src: url('../../css/font/iconfont.eot?#iefix') format('embedded-opentype'),
		url('../../css/font/iconfont.woff') format('woff'),
		url('../../css/font/iconfont.ttf') format('truetype'),
		url('../../css/font/iconfont.svg#iconfont') format('svg');
	}
	.icon {
		font-family:"iconfont" !important;
		font-size: 50upx;
		font-weight: 200;
		font-style:normal;
		-webkit-font-smoothing: antialiased;
		-webkit-text-stroke-width: 0.2px;
		-moz-osx-font-smoothing: grayscale;
		cursor: pointer;
		&:hover {
			color: #E80505;
		}
	}
	.add{
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		justify-content: center;
		align-items: center;
		.btn{
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-image:linear-gradient( 135deg, #FDD819 10%, #E80505 100%);
			color: #fff;
			justify-content: center;
			align-items: center;
			.icon{
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}
			font-size: 30upx;
		}
	}
	.list{
		flex-wrap: wrap;
		.row{
			width: 96%;
			padding: 20upx 2%;
			.left{
				width: 90upx;
				flex-shrink: 0;
				padding-top: 14upx;
				.head{
					width: 70upx;
					height: 70upx;
					background:linear-gradient(to right,#ccc,#aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 35upx;
				}
			}
			.center{
				width: 100%;
				flex-wrap: wrap;
				.name-tel{
					width: 100%;
					align-items: baseline;
					.name{
						font-size: 34upx;
					}
					.tel{
						margin-left: 30upx;
						font-size: 24upx;
						color: #777;
					}
					.default{
						font-size: 22upx;
						background-color: #E80505;
						color: #fff;
						padding: 0 18upx;
						border-radius: 24upx;
						margin-left: 20upx;
					}
				}
				.address{
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #777;
				}
			}
			.right{
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;
				.icon{
					font-size: 40upx;
					justify-content: center;
					align-items: center;
					width: 80upx;
					height: 60upx;
					border-left: solid 1upx #aaa;
					font-size: 40upx;
					color: #777;
				}
			}
		}
	}
	.no_address {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100%;
		color: #aaa;
		image {
			display: inline-block;
			width: 240upx;
			height: 240upx;
			margin: 14% auto;
		}
	}
</style>
